#!/bin/bash
source ~/command/common

oldname=
newname=
_oldname=""
_newname=""
_oldname1=""
_newname1=""
_oldname2=""
_newname2=""
_oldname3=""
_newname3=""
_oldname4=""
_newname4=""
_oldnameFlag=0
_newnameFlag=0

function showHelp() {
    echo "Usage:"
    echo "  xr oldname:newname[,oldname1:newname1] [-r|-c|-f]"
    echo "  默认替换剪切板的内容"
    echo "  -r: 进入某一个目录，将目录里面的文件按规则重命名，将文件中的内容按照规则替换"
    echo "  -c [folder] [force]: 拷贝目录后，再替换"
    echo "  -f [file]: 拷贝文件后，再替换"
    echo "  xr shop:client[,店铺:客户] -f|-c shopTail，结果：cp [-r] shopTail clientTail"
    echo "  xr Shop:client[,店铺:客户] -f|-c ShopTail，结果：cp [-r] ShopTail ClientTail"
    echo "  xr shop:client[,店铺:客户] -f|-c，结果：cp [-r] shop client"
    echo "  xr Shop:client[,店铺:客户] -f|-c，结果：cp [-r] Shop Client"
    echo "  替换规则：名称采用驼峰格式或者火箭式, 如: xr shopMemberModel:ClientModel 或者 xr ShopMemberModel:ClientModel"
    echo "  命令执行修改： shopMemberModel->clientModel; ShopMemberModel->ClientModel; SHOP_MEMBER_MODEL->CLIENT_MODEL shop_member_model->client_model"
    echo ""
    exit
}

pre="${1%,*}"
post="${1#*,}"
oldname="${pre%:*}"
newname="${pre#*:}"
_oldname4="${post%:*}"
_newname4="${post#*:}"

if [ "$oldname" = "$_oldname4" -a "$newname" = "$_newname4" ];then
    _oldname4="__not_exists_word__"
    _newname4="__not_exists_word__"
    if [ "$1" != "$oldname:$newname" -o "$oldname" = "" -o "$newname" = "" ];then
        showHelp
    fi
else
    if [ "$oldname:$newname,$_oldname4:$_newname4" != "$1" -o "$oldname" = "" -o "$newname" = "" -o "$_oldname4" = "" -o "$_newname4" = "" ];then
        showHelp
    fi
fi

echo $oldname | grep '^[A-Z]' > /dev/null
if [ $? -ne 0 ];then
    _oldnameFlag=1;
fi

echo $newname | grep '^[A-Z]' > /dev/null
if [ $? -ne 0 ];then
    _newnameFlag=1;
fi

if [ $_oldnameFlag = 1 ];then
    _oldname="$oldname"
    _oldname1=`echo $oldname|sed 's/^[a-z]/\u&/'`
else
    _oldname1="$oldname"
    _oldname=`echo $oldname|sed 's/^[A-Z]/\l&/'`
fi

if [ $_newnameFlag = 1 ];then
    _newname="$newname"
    _newname1=`echo $newname|sed 's/^[a-z]/\u&/'`
else
    _newname1="$newname"
    _newname=`echo $newname|sed 's/^[A-Z]/\l&/'`
fi
_oldname2=`echo $_oldname|sed 's/[A-Z]/_&/g'|sed 's/[a-z]/\u&/g'`
_newname2=`echo $_newname|sed 's/[A-Z]/_&/g'|sed 's/[a-z]/\u&/g'`
_oldname3=`echo $_oldname|sed 's/[A-Z]/_&/g'|sed 's/[A-Z]/\l&/g'`
_newname3=`echo $_newname|sed 's/[A-Z]/_&/g'|sed 's/[A-Z]/\l&/g'`

# 替换前切板的内容
if [ "$2" == "" ];then
    if __mac ;then
        pbpaste|sed 's/'${_oldname}'/'${_newname}'/g'|sed 's/'${_oldname1}'/'${_newname1}'/g'|sed 's/'${_oldname2}'/'${_newname2}'/g'|sed 's/'${_oldname3}'/'${_newname3}'/g'|sed 's/'${_oldname4}'/'${_newname4}'/g'|pbcopy
    else
        cat /dev/clipboard|sed 's/'${_oldname}'/'${_newname}'/g'|sed 's/'${_oldname1}'/'${_newname1}'/g'|sed 's/'${_oldname2}'/'${_newname2}'/g'|sed 's/'${_oldname3}'/'${_newname3}'/g'|sed 's/'${_oldname4}'/'${_newname4}'/g'  > /dev/clipboard
    fi
    exit
fi

if [ "$2" != "-r" -a "$2" != "-f" -a "$2" != "-c" ];then
    showHelp
fi

if [ "$2" = "-f" ];then
    oldfile=""
    newfile=""
    oldfilename=""
    newfilename=""
    if [ "$oldname" = "$_oldname" ];then
        oldfilename="$_oldname"
        newfilename="$_newname"
    elif [ "$oldname" = "$_oldname1" ];then
        oldfilename="$_oldname1"
        newfilename="$_newname1"
    fi
    if [ -z "$3" ];then
        oldfile="$oldfilename"
        newfile="$newfilename"
    else
        oldfile="$3"
        newfile="${3/${oldfilename}/${newfilename}}"
    fi
    if ! [ -f "$oldfile" ];then
        echo "$oldfile 不存在"
        exit
    fi
    cp $oldfile $newfile
    vim -e -s -c ":%s/${_oldname}/${_newname}/g" -c ":%s/${_oldname1}/${_newname1}/g" -c ":%s/${_oldname2}/${_newname2}/g" -c ":%s/${_oldname3}/${_newname3}/g"  -c ":%s/${_oldname4}/${_newname4}/g" -c ":wq" $newfile
    exit
fi


if [ "$2" = "-c" ];then
    olddir=""
    newdir=""
    olddirname=""
    newdirname=""
    if [ "$oldname" = "$_oldname" ];then
        olddirname="$_oldname"
        newdirname="$_newname"
    elif [ "$oldname" = "$_oldname1" ];then
        olddirname="$_oldname1"
        newdirname="$_newname1"
    fi
    if [ -z "$3" ];then
        olddir="$olddirname"
        newdir="$newdirname"
    else
        olddir="$3"
        newdir="${3/${olddirname}/${newdirname}}"
    fi
    if ! [ -d "$olddir" ];then
        echo "$olddir 不存在"
        exit
    fi
    cp -r $olddir $newdir
    cd $newdir
fi


echo xfind -n "*${_oldname}*" -r "${_oldname}:${_newname}"
echo xfind -v "-c \"%s/${_oldname}/${_newname}/g\""

echo xfind -n "*${_oldname1}*" -r "${_oldname1}:${_newname1}"
echo xfind -v "-c \"%s/${_oldname1}/${_newname1}/g\""

echo xfind -n "*${_oldname2}*" -r "${_oldname2}:${_newname2}"
echo xfind -v "-c \"%s/${_oldname2}/${_newname2}/g\""

echo xfind -n "*${_oldname3}*" -r "${_oldname3}:${_newname2}"
echo xfind -v "-c \"%s/${_oldname3}/${_newname2}/g\""


if ! [ "$4" = "force" ];then
    __ask "确定替换吗"
    if [ "$?" = "$_FALSE_" ];then
        exit
    fi
fi

xfind -n "*${_oldname}*" -r "${_oldname}:${_newname}"
xfind -v "-c \"%s/${_oldname}/${_newname}/g\""

xfind -n "*${_oldname1}*" -r "${_oldname1}:${_newname1}"
xfind -v "-c \"%s/${_oldname1}/${_newname1}/g\""

xfind -n "*${_oldname2}*" -r "${_oldname2}:${_newname2}"
xfind -v "-c \"%s/${_oldname2}/${_newname2}/g\""

xfind -n "*${_oldname3}*" -r "${_oldname3}:${_newname3}"
xfind -v "-c \"%s/${_oldname3}/${_newname3}/g\""

if [ "$_oldname4" != "__not_exists_word__" ];then
    xfind -v "-c \"%s/${_oldname4}/${_newname4}/g\""
fi
